package com.ebay.paisapay;

import java.sql.*;
import java.util.*;

import com.ebay.util.DBconn;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class transactiondetails extends ActionSupport {
	private String title;
	private String shippingmethod;
	private String quantity;
	private Float total;
	private String currentstatus;
	private Float allamountpaid=(float) 0.0 ,buyamountpaid=(float) 0.0 ,soldamountpaid=(float) 0.0 ;
	
	private String buytitle;
	private String buyshippingmethod;
	private String buyquantity;
	private Float buytotal;
	private String buycurrentstatus;
	
	String soldtitle;
	String soldshippingmethod;
	String soldquantity;
	Float soldtotal;
	String soldcurrentstatus;
	
	Map session;
	public ArrayList<transactiondetailsmodel> alllist, buylist, soldlist;
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	void allList() {
		String category_name;
		String subcategory_name;
		String product_id;
		String table_name;
		
		session=ActionContext.getContext().getSession();
		session.put("userID", "som");
		String myebay_id = (String) session.get("userID");
		conn = DBconn.getConnection();
		try {
			stmt = conn.createStatement();
			
			String sql = "Select C.category_name, S.subcategory_name, B.product_id, B.buyer_id, B.seller_id, B.quantity, B.amount, B.status From bank_third_party B, category C, subcategory S WHERE B.category_id=C.category_id and B.category_id=  S.category_id and B.subcategory_id=S.subcategory_id and (B.buyer_id='"+myebay_id+"' or B.seller_id='"+myebay_id+"')";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				transactiondetailsmodel m= new transactiondetailsmodel();
				category_name=rs.getString(1);
				subcategory_name=rs.getString(2);
				table_name=category_name+subcategory_name;
				product_id=rs.getString(3);
				m.setAllquantity(rs.getString(6));
				m.setAlltotal(rs.getFloat(7));
				int status= rs.getInt(8);
				if(status==0)
					m.setAllcurrentstatus("Payment Pending");
				else if(status==1)
					m.setAllcurrentstatus("Payment Done");
				else if(status==2)
					m.setAllcurrentstatus("Transfer Pending");
				else if(status==3)
					m.setAllcurrentstatus("Transfer Done");
				String sql_title = "SELECT P.title FROM " + table_name + " P WHERE P.product_id='"+ product_id +"'";
				Connection con_title= DBconn.getConnection();
				Statement stmt_title= con_title.createStatement();
				ResultSet rs_title = stmt_title.executeQuery(sql_title);
				if(rs_title.next()){
					m.setAlltitle(rs_title.getString(1));
				}
				DBconn.close(con_title);
				allamountpaid=allamountpaid+m.getAlltotal();
				System.out.println("Title All\t: "+ m.getAlltitle());
				System.out.println("Quantity All\t: " + m.getAllquantity());
				System.out.println("Total All\t: " + m.getAlltotal());
				System.out.println("All Current Status\t: " + m.getAllcurrentstatus());
				System.out.println("***********************************************");
				alllist.add(m);
			}
		} catch (Exception e) {
			System.err
					.println("Exception : In TransactionDetail.execute.allList()");
			e.printStackTrace();
		}
		DBconn.close(conn);

	}

	void buyList() {
		String category_name;
		String subcategory_name;
		String product_id;
		String table_name;
		
		session=ActionContext.getContext().getSession();
//		session.put("userID", "som");
		String myebay_id = (String) session.get("userID");
		conn = DBconn.getConnection();
		try {
			stmt = conn.createStatement();
			transactiondetailsmodel mn= new transactiondetailsmodel();
			String sql = "Select C.category_name, S.subcategory_name, B.product_id, B.buyer_id, B.seller_id, B.quantity, B.amount, B.status From bank_third_party B, category C, subcategory S WHERE B.category_id=C.category_id and B.category_id=  S.category_id and B.subcategory_id=S.subcategory_id and B.buyer_id='"+myebay_id+"'";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				category_name=rs.getString(1);
				subcategory_name=rs.getString(2);
				table_name=category_name+subcategory_name;
				product_id=rs.getString(3);
				mn.setBuyquantity(rs.getString(6));
				mn.setBuytotal(rs.getFloat(7));
				int status= rs.getInt(8);
				if(status==0)
					mn.setBuycurrentstatus("Payment Pending");
				else if(status==1)
					mn.setBuycurrentstatus("Payment Done");
				else if(status==2)
					mn.setBuycurrentstatus("Transfer Pending");
				else if(status==3)
					mn.setBuycurrentstatus("Transfer Done");
				String sql_title = "SELECT P.title FROM " + table_name + " P WHERE P.product_id='"+ product_id +"'";
				Connection con_title= DBconn.getConnection();
				Statement stmt_title= con_title.createStatement();
				ResultSet rs_title = stmt_title.executeQuery(sql_title);
				if(rs_title.next()){
					mn.setBuytitle(rs_title.getString(1));
				}
				DBconn.close(con_title);
				buyamountpaid=buyamountpaid+mn.getBuytotal();
//				System.out.println("Title Buy\t: "+ mn.getBuytitle());
//				System.out.println("Quantity Buy\t: " + mn.getBuyquantity());
//				System.out.println("Total Buy\t: " + mn.getBuytotal());
//				System.out.println("Current Status Buy\t: " + mn.getBuycurrentstatus());
//				System.out.println("******************Buy*****************************");
				buylist.add(mn);
			}
		} catch (Exception e) {
			System.err
					.println("Exception : In TransactionDetail.execute.allList()");
			e.printStackTrace();
		}
		DBconn.close(conn);


	}

	void soldList() {
		System.out.println("in soldList of transaction details");
		String category_name;
		String subcategory_name;
		String product_id;
		String table_name;
		
		session=ActionContext.getContext().getSession();
//		session.put("userID", "som");
		String myebay_id = (String) session.get("userID");
		conn = DBconn.getConnection();
		try {
			stmt = conn.createStatement();
			transactiondetailsmodel m= new transactiondetailsmodel();
			String sql = "Select C.category_name, S.subcategory_name, B.product_id, B.buyer_id, B.seller_id, B.quantity, B.amount, B.status From bank_third_party B, category C, subcategory S WHERE B.category_id=C.category_id and B.category_id=  S.category_id and B.subcategory_id=S.subcategory_id and B.seller_id='"+myebay_id+"'";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				category_name=rs.getString(1);
				subcategory_name=rs.getString(2);
				table_name=category_name+subcategory_name;
				product_id=rs.getString(3);
				m.setSoldquantity(rs.getString(6));
				m.setSoldtotal(rs.getFloat(7));
				int status= rs.getInt(8);
				if(status==0)
					m.setSoldcurrentstatus("Payment Pending");
				else if(status==1)
					m.setSoldcurrentstatus("Payment Done");
				else if(status==2)
					m.setSoldcurrentstatus("Transfer Pending");
				else if(status==3)
					m.setSoldcurrentstatus("Transfer Done");
				String sql_title = "SELECT P.title FROM " + table_name + " P WHERE P.product_id='"+ product_id +"'";
				Connection con_title= DBconn.getConnection();
				Statement stmt_title= con_title.createStatement();
				ResultSet rs_title = stmt_title.executeQuery(sql_title);
				if(rs_title.next()){
					m.setSoldtitle(rs_title.getString(1));
				}
				DBconn.close(con_title);
				soldamountpaid=soldamountpaid+m.getSoldtotal();
//				System.out.println("Title sold\t: "+ m.getSoldtitle());
//				System.out.println("Quantity sold\t: " + m.getSoldquantity());
//				System.out.println("Total sold\t: " + m.getSoldtotal());
//				System.out.println("Current Status\t: " + m.getSoldcurrentstatus());
//				System.out.println("***********************************************");
				soldlist.add(m);
			}
		} catch (Exception e) {
			System.err
					.println("Exception : In TransactionDetail.execute.allList()");
			e.printStackTrace();
		}
		DBconn.close(conn);


	}

	@Override
	public String execute() throws Exception {
		System.out.println("in execute of transaction details");
		alllist= new ArrayList<transactiondetailsmodel>();
		soldlist= new ArrayList<transactiondetailsmodel>();
		buylist= new ArrayList<transactiondetailsmodel>();
		allList();
		buyList();
		soldList();
//		for(transactiondetailsmodel o: buylist){
//			System.out.println(o.getAlltitle());
//		}
		return SUCCESS;
	}


	public String getBuytitle() {
		return buytitle;
	}

	public void setBuytitle(String buytitle) {
		this.buytitle = buytitle;
	}

	public String getBuyshippingmethod() {
		return buyshippingmethod;
	}

	public void setBuyshippingmethod(String buyshippingmethod) {
		this.buyshippingmethod = buyshippingmethod;
	}

	public String getBuyquantity() {
		return buyquantity;
	}

	public void setBuyquantity(String buyquantity) {
		this.buyquantity = buyquantity;
	}

	public Float getBuytotal() {
		return buytotal;
	}

	public void setBuytotal(Float buytotal) {
		this.buytotal = buytotal;
	}

	public String getBuycurrentstatus() {
		return buycurrentstatus;
	}

	public void setBuycurrentstatus(String buycurrentstatus) {
		this.buycurrentstatus = buycurrentstatus;
	}

	public String getSoldtitle() {
		return soldtitle;
	}

	public void setSoldtitle(String soldtitle) {
		this.soldtitle = soldtitle;
	}

	public String getSoldshippingmethod() {
		return soldshippingmethod;
	}

	public void setSoldshippingmethod(String soldshippingmethod) {
		this.soldshippingmethod = soldshippingmethod;
	}

	public String getSoldquantity() {
		return soldquantity;
	}

	public void setSoldquantity(String soldquantity) {
		this.soldquantity = soldquantity;
	}

	public Float getSoldtotal() {
		return soldtotal;
	}

	public void setSoldtotal(Float soldtotal) {
		this.soldtotal = soldtotal;
	}

	public String getSoldcurrentstatus() {
		return soldcurrentstatus;
	}

	public void setSoldcurrentstatus(String soldcurrentstatus) {
		this.soldcurrentstatus = soldcurrentstatus;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}

	public Float getTotal() {
		return total;
	}

	public void setTotal(Float total) {
		this.total = total;
	}

	public String getCurrentstatus() {
		return currentstatus;
	}

	public void setCurrentstatus(String currentstatus) {
		this.currentstatus = currentstatus;
	}

	public Float getAllamountpaid() {
		return allamountpaid;
	}

	public void setAllamountpaid(Float allamountpaid) {
		this.allamountpaid = allamountpaid;
	}

	public Float getBuyamountpaid() {
		return buyamountpaid;
	}

	public void setBuyamountpaid(Float buyamountpaid) {
		this.buyamountpaid = buyamountpaid;
	}

	public Float getSoldamountpaid() {
		return soldamountpaid;
	}

	public void setSoldamountpaid(Float soldamountpaid) {
		this.soldamountpaid = soldamountpaid;
	}

}
